import commands
import numpy as np
import sys


def main():
	if sys.argv[1][0] == 'B':
		regex = '{0}*'.format(sys.argv[1])
	elif sys.argv[1][0] == 'M':
		regex = '{0}*'.format(sys.argv[1])
	else:
		print 'Invalid Input.'
		return

	jsdirs = commands.getoutput('du -s {0}'.format(regex)).split('\n')
	stats = np.array([int(jsdir.split('\t')[0]) for jsdir in jsdirs])
	stats = np.log10(stats).astype(int)
	mvalue = 15
	histstats = np.histogram(stats, range(mvalue))[0]
	label = np.array([10**i for i in range(mvalue - 1)])
	result = np.column_stack((label, histstats))

	outf = open('{0}jssize.stats'.format(sys.argv[1]), 'w')
	for row in result:
		if not row[1] == 0:
			outf.write(str(row[0]) + ',' + str(row[1]) + '\n')
	print label
	print histstats
	print result
	outf.close()


if __name__=="__main__":
        main()
